package com.zn.web.rpc;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zn.util.Constant;
import com.zn.util.HttpClientUtil;
import com.zn.vo.AccountInfo;
import com.zn.vo.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.TimeZone;

/**
 * @author YinJiaWei
 * @version 2018/8/25 20:12
 */
@Component
public class TradeRemoteCall {
    private static final Logger LOGGER = LoggerFactory.getLogger(TradeRemoteCall.class);
    private static final String ADDRESS_IP="http://119.27.165.142";
    private static final Integer PORT=18007;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    private static final ObjectMapper MAPPER = new ObjectMapper();
    static {
        MAPPER.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    }

    /**
     * 查询所有交易合约信息
     * @return
     */
    public Response allHy(){
        Response response=HttpClientUtil.get(ADDRESS_IP+":"+PORT,
                HttpClientUtil.Builder.builder("/all/hy")
        );
        return response;
    }

    /**
     * API 交易账户登录
     * @param uid
     * @param password
     * @return
     */
    public AccountInfo login(Long uid,String password){
        try {
//            Map<String,Object> response=HttpClientUtil.post(
//                    HttpClientUtil.Builder.builder("/veripass")
//                            .with("uid", uid)
//                            .with("pass", password)
//                    , ADDRESS_IP
//            );
//            String cookie=response.get("cookie").toString();
//            String json=response.get("data").toString();
//            AccountInfo accountInfo=MAPPER.readValue(json, AccountInfo.class);
//            accountInfo.setCookie(cookie);
//            Object accountInfo2Json= JSONObject.toJSON(accountInfo);
//            redisTemplate.opsForHash().put(Constant.ACCOUNT_INFO_LIST, uid.toString(), accountInfo2Json.toString());
//            Object maxIdx=redisTemplate.opsForHash().get(Constant.OPTION_ORDER_MAX_IDX,uid);
//            if(maxIdx==null){
//                redisTemplate.opsForHash().put(Constant.OPTION_ORDER_MAX_IDX,uid,0);
//            }
//            return accountInfo;
            return null;
        }catch (Exception e){
            LOGGER.error("login.........",e);
            throw new RuntimeException(e);
        }
    }

    /**
     *
     * @param id 客户编号
     * @param idx 成交起始序号
     * @return
     */
    public Response getTurnover(Integer id,Integer idx){
        Response response=HttpClientUtil.get(ADDRESS_IP+":"+PORT,
                HttpClientUtil.Builder.builder("/cj/kh")
                .with("id",id)
                .with("from",idx+1)

        );
        return response;
    }
}
